//
//  PrinterManageVC.m
//  dyseller
//
//  Created by 红喇叭 on 2017/11/6.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "PrinterManageVC.h"
#import "SetWiFiStampVC.h"
#import "myWkWebViewController.h"

@interface PrinterManageVC ()<UITableViewDataSource,UITableViewDelegate>{
    NSMutableArray *_facilityArr;//设备名称列表
    NSInteger stamptag;//记录开关的设备
}
@property (weak, nonatomic) IBOutlet UIView *blueView;
@property (weak, nonatomic) IBOutlet UIView *helpView;
@property (weak, nonatomic) IBOutlet UIView *addView;
@property (weak, nonatomic) IBOutlet UISwitch *controlSwitch;
@property(nonatomic,strong)UITableView *printerTable;
@property (weak, nonatomic) IBOutlet UILabel *reminderLab;

@end

@implementation PrinterManageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
   [self setNavTitle:@"打印机设置" titleColor:[UIColor blackColor] font:font17];
    
   
    _blueView.backgroundColor = self.view.backgroundColor;
    
    [self.blueView addSubview:self.printerTable];
    
    
    //添加打印机的事件
    UITapGestureRecognizer *addtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addStamp)];
    [_addView addGestureRecognizer:addtap];
    
    
    //帮助的事件
    UITapGestureRecognizer *helptap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(helpViewTap)];
    [_helpView addGestureRecognizer:helptap];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getPrinterList];
}

- (void)getPrinterList{
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Print/printerList" isactive:YES parameter:@{} successBlock:^(PPHttpRequest *request, id responseObj) {
        NSString *auto_printer = responseObj[@"is_auto_printer"];
        _controlSwitch.on = [auto_printer isEqualToString:@"0"]?NO:YES;
        _facilityArr = responseObj[@"printer_list"];
        [self.printerTable reloadData];
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
         [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];
}

- (void)viewDidLayoutSubviews{
    _printerTable.frame = _blueView.bounds;
}

- (UITableView *)printerTable{
    if(_printerTable==nil){
        _printerTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _printerTable.delegate = self;
        _printerTable.dataSource = self;
        _printerTable.estimatedSectionFooterHeight = 0 ;
        _printerTable.estimatedSectionHeaderHeight = 0;
        _printerTable.estimatedRowHeight = 60;
        _printerTable.backgroundColor = self.view.backgroundColor;
        [_printerTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _printerTable;
}
#pragma  mark ========全部打印机是否自动打印========
- (IBAction)controlAction:(id)sender {
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Print/setAutoPrinter" parameter:@{@"is_auto_printer":_controlSwitch.on?@"1":@"0"} successBlock:^(PPHttpRequest *request, id responseObj) {
        
        
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
         _controlSwitch.on = !_controlSwitch.on;
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];
}


#pragma  mark ========添加打印机========
-(void)addStamp{
    SetWiFiStampVC *vc = [SetWiFiStampVC new];
    vc.isNewStamp = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

//帮助
- (void)helpViewTap{
    myWkWebViewController *vc = [[myWkWebViewController alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/ShopApi/Html/itemDetail?noticeId=191&flag=1",APP_HOST_ADDRESS];
    vc.myurl = url;
    vc.toTitle = @"公告详情";
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma  mark ========UITableView 的代理========
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  _facilityArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    NSDictionary *dic = _facilityArr[indexPath.row];
    UILabel *snName = [[UILabel alloc] init];
    snName.text = dic[@"printer_sn"];
    [cell.contentView addSubview:snName];
 snName.sd_layout.leftEqualToView(_reminderLab).centerYEqualToView(cell.contentView).widthIs(0).autoHeightRatio(0);
    [snName setSingleLineAutoResizeWithMaxWidth:200];
    
    UISwitch *snSwitch = [[UISwitch alloc] init];
    snSwitch.tag = indexPath.row;
    snSwitch.onTintColor = [UIColor redColor];
    if([dic[@"is_enable"] isEqualToString:@"1"]){
        snSwitch.on = YES;
    }else{
        snSwitch.on = NO;
    }
    [cell.contentView addSubview:snSwitch];
 snSwitch.sd_layout.rightEqualToView(_controlSwitch).centerYEqualToView(cell.contentView).widthRatioToView(_controlSwitch, 1).heightRatioToView(_controlSwitch, 1);
    [snSwitch addTarget:self action:@selector(controllerSingleStamp:) forControlEvents:UIControlEventTouchUpInside];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *herderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W,40)];
    herderView.backgroundColor = self.view.backgroundColor;
    UILabel *stampLab = [[UILabel alloc] init];
    [herderView addSubview:stampLab];
    stampLab.font = [UIFont systemFontOfSize:15];
    stampLab.textColor = [UIColor lightGrayColor];
    if(_facilityArr.count>0){
        stampLab.text = @"当前已配对设备";
    }else{
        stampLab.text = @"暂无配对设备";
    }
 stampLab.sd_layout.leftEqualToView(_reminderLab).centerYEqualToView(herderView).widthIs(150).autoHeightRatio(0);
    
    return herderView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = _facilityArr[indexPath.row];
    if(![dic[@"is_enable"] isEqualToString:@"1"]){
        [AlertUtils showToastWithMessage:@"请先开启打印机" inView:self.view hideAfterDelay:1.0 beIndeter:NO];
        return;
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SetWiFiStampVC *vc = [SetWiFiStampVC new];
    vc.isNewStamp = NO;
    vc.stampInfo = dic.mutableCopy;
    
    [self.navigationController pushViewController:vc animated:YES];

}

#pragma  mark ========单个设备开关========
- (void)controllerSingleStamp:(UISwitch *)sender{
   
    NSDictionary *dic = (_facilityArr[sender.tag]);
    stamptag = sender.tag;
    NSMutableDictionary *newdic = dic.mutableCopy;
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Print/setPrinter" parameter:@{@"printer_key":dic[@"printer_key"],@"printer_sn":dic[@"printer_sn"],@"is_enable":sender.on?@"1":@"0",@"printer_type":@"2"} successBlock:^(PPHttpRequest *request, id responseObj) {
        newdic[@"is_enable"] = sender.on?@"1":@"0";
        
        NSMutableArray *arr = _facilityArr.mutableCopy;
        arr[stamptag] = newdic;
        _facilityArr = arr;
        
        [self.printerTable reloadData];
        [AlertUtils showToastWithMessage:@"设置成功" inView:self.view hideAfterDelay:1.0 beIndeter:NO];
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
        sender.on = !sender.on;
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];
}


@end
